package cn.stylefeng.guns.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.common.annotion.Permission;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.modular.system.entity.Coupon;
import cn.stylefeng.guns.modular.system.model.CouponDto;
import cn.stylefeng.guns.modular.system.service.CouponService;
import cn.stylefeng.guns.modular.system.warpper.CouponWarpper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Map;

/**
 * 优惠卷控制器
 */
@Controller
@RequestMapping("/coupon")
public class CouponController extends BaseController {
    private static String PREFIX = "/modular/system/coupon/";

    @Autowired
    private CouponService couponService;

    /**
     * 跳转到查看兑换劵列表的页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "coupon.html";
    }


    /**
     * 跳转到兑换劵生成的页面
     */
    @RequestMapping("coupon_generate")
    public String generateCoupon() {
        return PREFIX + "coupon_generate.html";
    }

    /**
     * 跳转到兑换劵添加页面
     * ps：必须填写玩着的信息
     */
    @RequestMapping("coupon_add")
    public String couponAdd() {
        return PREFIX + "coupon_add.html";
    }

    /**
     * 跳转到修改角色
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @Permission
    @RequestMapping(value = "/coupon_edit")
    public String couponEdit(@RequestParam Long id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        return PREFIX + "/coupon_edit.html";
    }


    /**
     * 导出Excel
     */
    @Permission
    @RequestMapping(value = "/export_excel")
    @ResponseBody
    public ResponseData exportExcel(@RequestParam Long ids[]) {
        if (ToolUtil.isEmpty(ids)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        couponService.exportExcel(ids);
        return SUCCESS_TIP;
    }

    /**
     * 删除兑换劵
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @RequestMapping(value = "/remove")
    @Permission
    @ResponseBody
    public ResponseData remove(@RequestParam Long id) {
        this.couponService.removeById(id);
        return SUCCESS_TIP;
    }

    /**
     * 查看兑换劵
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @RequestMapping(value = "/view/{id}")
    @Permission
    @ResponseBody
    public ResponseData view(@PathVariable Long id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        Coupon coupon = this.couponService.getById(id);
        Map<String, Object> couponMap = BeanUtil.beanToMap(coupon);
        return ResponseData.success(couponMap);
    }


    /**
     * 兑换劵修改
     **/
    @RequestMapping(value = "/edit")
    @Permission
    @ResponseBody
    public ResponseData edit(@Valid CouponDto couponDto, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.couponService.editCoupon(couponDto);
        return SUCCESS_TIP;
    }

    /**
     * 兑换劵添加
     */
    @Permission
    @RequestMapping("add")
    @ResponseBody
    public ResponseData add(@Valid CouponDto couponDto, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        couponService.addCoupon(couponDto);
        return SUCCESS_TIP;
    }

    /**
     * 兑换劵生成
     */
    @Permission
    @RequestMapping("generate")
    @ResponseBody
    public ResponseData generate(@RequestParam Integer length, @RequestParam Integer count) {

        if (length < 10 || length > 100 || count < 1 || count > 500) {
            throw new ServiceException(BizExceptionEnum.REQUEST_INVALIDATE);
        }
        couponService.generateCoupon(length, count);
        return SUCCESS_TIP;
    }

    /**
     * 兑换劵列表
     */
    @RequestMapping("/list")
    @Permission
    @ResponseBody
    public Object list(@RequestParam(required = false) String status,
                       @RequestParam(required = false) String timeLimit,
                       @RequestParam(required = false) String condition) {
        //拼接查询条件
        String beginTime = "";
        String endTime = "";

        if (ToolUtil.isNotEmpty(timeLimit)) {
            String[] split = timeLimit.split(" - ");
            beginTime = split[0];
            endTime = split[1];
        }
        Page<Map<String, Object>> coupons = couponService.selectCoupons(status, beginTime, endTime, condition);
        Page wrapped = new CouponWarpper(coupons).wrap();
        return LayuiPageFactory.createPageInfo(wrapped);
    }
}
